package com.library.bookmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bookmanagement.entity.Admin;
import com.library.bookmanagement.entity.Book;
import com.library.bookmanagement.entity.Read;
import com.library.bookmanagement.entity.User;
import com.library.bookmanagement.mapper.*;
import com.library.bookmanagement.service.AdminService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/8 16:52
 * @description:图书管理员service接口实现类
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookSituationMapper bookSituationMapper;

    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private ReadMapper readMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
    * @Description: 查询所有管理员
    * @Param: []
    * @return: java.util.List<com.library.bookmanagement.entity.Admin>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public List<Admin> QueryAdminAll() {
        return adminMapper.selectAdminAll();
    }

    /**
    * @Description: 通过管理员id删除管理员
    * @Param: [id]
    * @return: java.lang.Boolean
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public Boolean deleteById(Integer id) {
        Boolean i = adminMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
    * @Description: 添加管理员
    * @Param: [admin]
    * @return: java.lang.Boolean
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public Boolean addAdmin(Admin admin) {
        Boolean i = adminMapper.insert(admin);
        return true;
    }

    /*@Override
    public Integer queryByEmail(String email) {
        return adminMapper.selectAdminCountByEmail(email);
    }
*/

    /**
    * @Description: 添加用户
    * @Param: [user]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil addUser(User user) {
        // 默认信息，可在用户个人中心修改
        user.setuSex("男");
        user.setuNumber(0);
        user.setOverNumber(0);
        user.setuBoolean(0);
        user.setRole(1);
        user.setuPassword(bCryptPasswordEncoder.encode(user.getuPassword()));
        userMapper.insert(user);
        return ResultUtil.ok().message("添加用户成功");
    }

    /**
    * @Description: 删除用户
    * @Param: [ids]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil deleteUserById(List<Integer> ids) {
        for(Integer id:ids){
            userMapper.deleteByPrimaryKey(id);
        }
        return ResultUtil.ok().message("用户删除成功");
    }

    /**
    * @Description: 查询所有用户
    * @Param: [pageNum, pageSize]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String,Object>> queryCanUse(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<User> users = userMapper.selectCanUse();
        if(users.getTotal() != 0){
            HashMap<String,Object> map = new HashMap<>();
            map.put(Constant.MESSAGE_TOTALNUM, users.getTotal());
            for(User user:users){
                Page<Read> reads = readMapper.selectByUserKey(user.getuId());
                ArrayList<String> bookNames = new ArrayList<>();
                for (Read read:reads){
                    Book book = read.getBook();
                    bookNames.add(book.getbName());
                }
                user.setBookNames(bookNames);
            }
            map.put(Constant.MEASSAGE_DATAS,users);
            return ResultUtil.ok().data(map);
        }
        return ResultUtil.error().message("用户为空");
    }

    /**
    * @Description: 添加图书
    * @Param: [book]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil addBook(Book book) {
        book.setIcurrentTime(new Date(System.currentTimeMillis()));
        book.setbBoolean(0);
        book.setBorrowCount(0);
        bookMapper.insert(book);
        return ResultUtil.ok().message("图书添加成功");
    }

    /**
    * @Description: 删除图书
    * @Param: [ids]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil deleteBookById(List<Integer> ids) {
        for (Integer id:ids){
            bookMapper.deleteByPrimaryKey(id);
        }
        return ResultUtil.ok().message("删除图书信息成功");
    }

    /**
    * @Description: 显示所有图书
    * @Param: [pageNum, pageSize]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String,Object>> queryBookIsLoad(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> books = bookMapper.selectBookIsLoad();
        if(books.getTotal() != 0){
            HashMap<String,Object> map = new HashMap<>();
            map.put(Constant.MESSAGE_TOTALNUM,books.getTotal());
            map.put(Constant.MEASSAGE_DATAS,books);
            return ResultUtil.ok().data(map);
        }
        return ResultUtil.error().message("显示图书信息失败");
    }

    /**
    * @Description: 显示所有用户
    * @Param: [userId]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.User>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<User> queryUserById(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user != null){
            return ResultUtil.ok().data(user);
        }else{
            return ResultUtil.error().message("显示用户信息错误");
        }
    }

    /**
    * @Description: 修改用户基本信息
    * @Param: [user]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil editUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        return ResultUtil.ok().message("修改用户信息成功!");
    }

    /**
    * @Description: 返回图书信息
    * @Param: [bookId]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.Book>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Book> queryBookById(Integer bookId) {
        Book book = bookMapper.selectByPrimaryKey(bookId);
        if(book != null){
            return ResultUtil.ok().data(book);
        }else{
            return ResultUtil.error().message("显示图书信息错误");
        }
    }

    /**
    * @Description: 修改图书信息
    * @Param: [book]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil editBook(Book book) {
        bookMapper.updateByPrimaryKeySelective(book);
        return ResultUtil.ok().message("修改图书信息成功！");
    }

    /**
    * @Description: 修改个人信息
    * @Param: [admin]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil editAdminById(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
        return ResultUtil.ok().message("修改个人信息成功!");
    }

    /**
    * @Description: 管理员修改密码
    * @Param: [userId, oldPassword, newPassword]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil editUserPassword(Integer userId, String oldPassword, String newPassword) {
        Admin admin = adminMapper.selectByPrimaryKey(userId);
        if(!bCryptPasswordEncoder.matches(oldPassword,admin.getPassword())){
            return ResultUtil.error().message("旧密码不匹配!");
        }else{
            admin.setPassword( bCryptPasswordEncoder.encode(newPassword));
           adminMapper.updateByPrimaryKeySelective(admin);
            return ResultUtil.ok().message("密码修改成功!");
        }
    }
    
    /**
    * @Description: 重置读者密码
    * @Param: [user]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil editUserPaswword(User user) {
        user.setuPassword(bCryptPasswordEncoder.encode(user.getuPassword()));
        userMapper.updateByPrimaryKeySelective(user);
        return ResultUtil.ok().message("密码修改成功!");
    }

    /**
    * @Description: 根据管理员名称查询管理员
    * @Param: [username]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.Admin>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Admin> queryAdminByName(String username) {
        Admin admin = adminMapper.selectByName(username);
        return ResultUtil.ok().data(admin);
    }

    /**
    * @Description: 根据图书Id进行书籍下架
    * @Param: [bookId]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/12
    */
    @Override
    public ResultUtil pullOffBookById(Integer bookId, Integer bool) {
        Book book = bookMapper.selectByPrimaryKey(bookId);
        if(book != null){
            book.setbBoolean(bool);
            if(bool == 1){
                book.setLastTime(new Date(System.currentTimeMillis()));
            }else{
                book.setIcurrentTime(new Date(System.currentTimeMillis()));
            }
            bookMapper.updateByPrimaryKeySelective(book);
            return ResultUtil.ok().message("图书上下架成功!");
        }else{
            return ResultUtil.error().message("图书上下架操作失败!");
        }


    }

    /**
    * @Description: 根据用户ID注销用户
    * @Param: [userId]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/12
    */
    @Override
    public ResultUtil logOffUser(Integer userId,Integer bool) {
        User user = userMapper.selectByPrimaryKey(userId);
        if(user != null){
            user.setuBoolean(bool);
            return ResultUtil.ok().message("用户状态修改成功!");
        }else{
            return ResultUtil.error().message("用户状态修改失败!");
        }

    }

    /**
    * @Description: 根据用户名修改用户头像
    * @Param: [username, filename]
    * @return: void
    * @Author: ZXM
    * @Date: 2020/6/13
    */
    @Override
    public void editAdminByName(String username, String filename) {
        Admin admin = adminMapper.selectByName(username);
        if(admin != null ){
            admin.setImgUrl(filename);
            adminMapper.updateByPrimaryKeySelective(admin);
        }
    }

}
